const adminModel = require('../model/adminModle.js');



// 注册
module.exports.insert = async function (adminUser) {
    return await adminModel.create(adminUser)
}
// 查找用户名
module.exports.findByUsername = async function (adminUser) {
    let account = adminUser.account
    return await adminModel.find({ account })
}

// 查找所有
module.exports.findAdminUser = async function (adminUser, page) {
    //分页查询
    let { current, size } = page;
    //查询总数
    let total = await adminModel.countDocuments(adminUser);

    //计算总页数
    let totalPage = Math.ceil(total / size);
    //分页查询
    let rows = await adminModel.find(adminUser).limit(parseInt(size)).skip((current - 1) * size)
    return { current, size, total, totalPage, rows }

}

module.exports.findUserAll = async function () {
    return await adminModel.find()
}
// 修改
module.exports.update = async function (id, adminUser) {
    console.log(id, "id", "adminUser", adminUser, "dao");
    let data = await adminModel.updateOne({ _id: id }, adminUser)
    console.log(data, "dao");
    return data
}

//删除用户
module.exports.delete = async function(id){
    return await adminModel.deleteOne({_id:id})
}

//根据id查找
module.exports.findById=async function(id){
    return await adminModel.findById(id)
}